﻿using System;
using Castle.Core;
using Castle.MicroKernel;
using CModel = Castle.Core.ComponentModel;

namespace Mbs.CastleContainer
{
    public interface IKernelListner:IListner<KernelListnerType>
    {
        void OnAddedAsChildKernel(object sender, EventArgs e);
        void OnComponentCreated(CModel model, object instance);
        void OnComponentDestroyed(CModel model, object instance);
        void OnComponentModelCreated(CModel model);
        void OnComponentRegistered(string key, IHandler handler);
        void OnComponentUnregistered(string key, IHandler handler);
        void OnDependencyResolving(CModel client, DependencyModel model, object dependency);
        void OnHandlerRegistered(IHandler handler, ref bool stateChanged);
        void OnRemovedAsChildKernel(object sender, EventArgs e);
    }

  

    public class KernelListnerAdapter : Listner<KernelListnerType>, IKernelListner
    {
        protected KernelListnerAdapter(KernelListnerType type) : base(type) { }
        public virtual void OnAddedAsChildKernel(object sender, EventArgs e) { }
        public virtual void OnComponentCreated(CModel model, object instance) { }
        public virtual void OnComponentDestroyed(CModel model, object instance) { }
        public virtual void OnComponentModelCreated(CModel model) { }
        public virtual void OnComponentRegistered(string key, IHandler handler) { }
        public virtual void OnComponentUnregistered(string key, IHandler handler) { }
        public virtual void OnDependencyResolving(CModel client, DependencyModel model, object dependency) { }
        public virtual void OnHandlerRegistered(IHandler handler, ref bool stateChanged) { }
        public virtual void OnRemovedAsChildKernel(object sender, EventArgs e) { }
    }

    [Flags]
    public enum KernelListnerType
    {
        Undefine = 1,
        AddedAsChildKernel = 2,
        ComponentCreated = 4,
        ComponentDestroyed = 8,
        ComponentModelCreated = 16,
        ComponentRegistered = 32,
        ComponentUnregistered = 64,
        DependencyResolving = 128,
        HandlerRegistered = 256,
        RemovedAsChildKernel = 512,
    }
}
